package com.edu.learning.concurrent.sample.mode3;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * @author ll
 * @Date 2017/10/25 15:34
 */
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadFactory threadFactory =
                new ThreadFactoryBuilder().setDaemon(true).setNameFormat("CallableDemo-%d").build();
        ExecutorService executorService = new ThreadPoolExecutor(
                32,
                32,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024),
                threadFactory,
                new ThreadPoolExecutor.CallerRunsPolicy());

        Future<Integer> future = executorService.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("Callable is running");
                TimeUnit.SECONDS.sleep(2);
                return 27;
            }
        });
        executorService.shutdown();
        System.out.println("future.get = " + future.get());
    }
}
